<?php
namespace jy_goodserpfw;
defined('BY_JYA') or exit('error');
/*
	经销商
*/
class merch extends \plugin{
	function upkulog($post=array(),$mid=0){
		$codes=$post['codes'];
		$type=$post['type'];
		$code_type=1;
		if($post['type']==6){
			$type=0;
			$code_type=0;
		}elseif($post['type']==10){
			$type=1;
			$code_type=0;
		}elseif($post['type']==30){
			$type=3;
			$code_type=0;
		}
		$loglist=array();
		if(!$post['tomid'] && $post['tomobile']){
				$member=array(
					'uniacid'=>$_SESSION['uniacid'],
					'username'=>$post['tomobile'],
					'mobile'=>$post['tomobile'],
					'fromuser'=>$_SESSION['uid'],
				);
				if($post['tomobile']){
					$check=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>$post['tomobile']),array('id','fromuser'));
					$post['tomid']=$check['id'];
					if(!$post['tomid']){
						$check=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$post['tomobile']),array('id','fromuser'));
						$post['tomid']=$check['id'];
					}
				}
				if(!$post['tomid']){
					pdo_insert('core_members',$member);
					$post['tomid']=pdo_insertid();
					$user=pdo_get('jy_weishop_member',array('mid'=>$post['tomid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
					if(!$user['id']){
						$insert=array(
							'mid'=>$post['tomid'],
							'uniacid'=>$_SESSION['uniacid'],
							'createtime'=>TIMESTAMP,
						);
						model('jy_weishop/member')->register($insert);
					}
				}
		}
		$user=pdo_get('jy_goodserpfw_members',array('mid'=>$post['tomid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
		if(!$user['id']){
			$insert=array(
					'mid'=>$post['tomid'],
					'uniacid'=>$_SESSION['uniacid'],
					'nickname'=>$post['nickname'],
					'mobile'=>$post['tomobile'],
					'cname'=>$post['cname'],
					'address'=>$post['address'],
					'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_goodserpfw_members',$insert);
		}else{
			$update=array(
				'nickname'=>$post['nickname'],
				'mobile'=>$post['tomobile'],
				'cname'=>$post['cname'],
				'address'=>$post['address'],
			);
			pdo_update('jy_goodserpfw_members',$update,array('mid'=>$post['tomid'],'uniacid'=>$_SESSION['uniacid']));
		}
		foreach($codes as $cv){
			$cv['code']=trim($cv['code']);
			$log=array(
				'type'=>$type,
				'code'=>$cv['code'],
				'code_type'=>$code_type,
			);
			$this->model('model')->check_status($log,1);
		}
		foreach($codes as $cv){
			$cv['code']=trim($cv['code']);
			//$kulog=pdo_get('jy_goodserpfw_code',array('code'=>$cv['code'],'uniacid'=>$_SESSION['uniacid']));
			if($kulog['merchid']!=$post['merchid']){
				//exi($cv['code'].'标签的货不属于你！你未进货该标签的商品！','error');
			}
			if(!$code_type){
				$item=pdo_get('jy_goodserpfw_code',array('code'=>$cv['code'],'uniacid'=>$_SESSION['uniacid']));
				$code_num=1;
				$item['pre']=pdo_getcolumn('jy_goodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['batch_id']),'pre');
			}else{
				$item=pdo_get('jy_goodserpfw_xcode',array('code'=>$cv['code'],'uniacid'=>$_SESSION['uniacid']));
				$item['beinum']=$item['endnum']-$item['startnum']+1;
				$code_num=$item['beinum'];
				$item['pre']=pdo_getcolumn('jy_goodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['batch_id']),'xpre');
			}
			if(!$item['id']){
				exi($code_type?$cv['code'].'标签不属于箱标签':$cv['code'].'标签不属于盒标签','error');
			}
			if($item['merchid']!=$post['merchid']){
				exi($cv['code'].'标签的货不属于你！你未进货该标签的商品。','error');
			}
			$isadd=pdo_get('jy_goodserpfw_merchlog',array('type'=>$type,'code'=>$cv['code'],'uniacid'=>$_SESSION['uniacid']));
			if($isadd['id']){
				//exi($cv['code'].'标签重复操作！','error');
			}
			$goods_id=$item['goods_id'];
			$batch_id=$item['batch_id'];
			$item['sn']=sprintf("%08d", $item['sn']);
			$item['presn']=$item['pre'].$item['sn'];
			$sn=$item['presn'];
			$xiang_id=$item['xiang_id'];
			$log=array(
				'type'=>$type,
				'code'=>$cv['code'],
				'merchid'=>$post['merchid'],
				'storeid'=>$post['storeid'],
				'workerid'=>$post['workerid'],
				'goods_id'=>$goods_id,
				'batch_id'=>$batch_id,
				'xiang_id'=>$xiang_id,
				'sn'=>$sn,
				'code_type'=>$code_type,
				'xiang_num'=>1,
				'code_num'=>$code_num,
				'remark'=>$post['remark'],
				'tomid'=>$post['tomid'],
			);
			$log['uniacid']=$_SESSION['uniacid'];
			$log['createtime']=TIMESTAMP;
			$this->model('model')->upcodelog_status($log,1);
			pdo_insert('jy_goodserpfw_merchlog',$log);
			$log['id']=pdo_insertid();
			$loglist[]=$log;
		}
		if($type==1){
			$this->addfalog($loglist,$post['merchid']);
		}
		return $post['tomid'];
	}
	function update($goods_id,$data){
		$goods=array_fields(array('title','price','image','content','url'),$data);
		pdo_update('jy_goodserpfw_goods',$goods,array('id'=>$goods_id));
	}
	function addfalog($list,$merchid=0){
		if(!$list){
			exi('不存在出库记录！','error');
		}
		foreach($list as &$item){
			$item['total']=$item['code_type']?$item['xiang_num']:$item['code_num'];
			$item['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
		}
		$falog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$merchid,
			'kulog'=>ijson_encode($list),
			'createtime'=>time(),
		);
		$this->insert('merchfalog',$falog);
		foreach($list as $v){
			pdo_update('jy_goodserpfw_merchlog',array('status'=>1),array('id'=>intval($v['id']),'uniacid'=>$_SESSION['uniacid']));
		}
		return '';
	}
}